﻿using System;
using System.Windows.Forms;
using MetadataDocumentationGenerator.Properties;

namespace MetadataDocumentationGenerator.Controls
{
    /// <summary>
    /// User control for generation type selection
    /// </summary>
    public partial class Step1 : UserControl, IStepControl
    {
        #region Constructor

        /// <summary>
        /// Initializes a new instance of class Step1
        /// </summary>
        public Step1()
        {
            InitializeComponent();

            ReviewSettings = true;
        }

        #endregion Constructor

        #region Properties

        public bool ReviewSettings { get; private set; }

        #endregion Properties

        #region Methods

        private void RdbUseExisitingCheckedChanged(object sender, EventArgs e)
        {
            btnBrowse.Enabled = rdbUseExisiting.Checked;
            chkReviewSettings.Enabled = rdbUseExisiting.Checked;

            if(!rdbUseExisiting.Checked)
            {
                txtSettingsFilePath.Text = string.Empty;
                chkReviewSettings.Checked = false;
                pnlError.Visible = false;
            }
        }

        private void BtnBrowseClick(object sender, EventArgs e)
        {
            var ofd = new OpenFileDialog
                          {
                              Title = Resources.SettingsDialogTitle,
                              Filter = Resources.SettingsDialogFilter
                          };

            if(ofd.ShowDialog(this) == DialogResult.OK)
            {
                txtSettingsFilePath.Text = ofd.FileName;
                pnlError.Visible = false;
            }
        }

        private void TxtSettingsFilePathTextChanged(object sender, EventArgs e)
        {
            if (pnlError.Visible && txtSettingsFilePath.Text.Length > 0)
                pnlError.Visible = false;
        }

        public GenerationSettings GetSettings()
        {
            try
            {
                if (rdbUseExisiting.Checked && txtSettingsFilePath.Text.Length == 0)
                {
                    pnlError.Visible = true;
                    return null;
                }

                if (rdbUseExisiting.Checked)
                {
                    ReviewSettings = chkReviewSettings.Checked;
                    return GenerationSettings.CreateFromFile(txtSettingsFilePath.Text);
                }

                ReviewSettings = true;
                return new GenerationSettings();
            }
            catch
            {
                lblError.Text = Resources.InvalidTemplateErrorMessage;
                pnlError.Visible = true;
            }

            return null;
        }

        #endregion Methods
    }
}
